import numpy as np
import scipy.io as sio

data = sio.loadmat('ex3data1.mat')
raw_X = data['X']
raw_y = data['y']

X = np.insert(raw_X, 0, values=1, axis=1)
y = raw_y.flatten()
print(X.shape)  # (5000, 401)
print(y.shape)  # (5000,)

theta = sio.loadmat('ex3weights.mat')
print(theta.keys())

theta1 = theta['Theta1']
theta2 = theta['Theta2']
print(theta1.shape)  # (25, 401)
print(theta2.shape)  # (10, 26)


def sigmoid(z):
    return 1 / (1 + np.exp(-z))


a1 = X  # 第一次输入特征
z2 = X @ theta1.T
a2 = sigmoid(z2)

print(a2.shape)  # (5000, 25)

a2 = np.insert(a2, 0, values=1, axis=1)

z3 = a2 @ theta2.T
a3 = sigmoid(z3)
print(a3.shape)  # (5000, 10)

y_pred = np.argmax(a3, axis=1)
y_pred = y_pred + 1

acc = np.mean(y_pred == y)
print(acc)  # 0.9752




